#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
char* reverseStr(char* str,int start, int end);
char* reverseWord(const char* str);
int main ()
{
    //char a[] = {'L','O','V','E','\0'};
    //rereverseStr(A);
    char *a = "I love you,xiaomei!";
    char *b = reverseWord(a);
    delete b;
    cout<<a<<endl;
}

char* reverseWord(const char* str)
{
    if(NULL == str)return NULL;
    char* p = new char[strlen(str)+1];
    strcpy(p,str);
    if(strlen(str) == 1)return p;
    
    //reverse str
    p = reverseStr(p,0,strlen(str));
    cout <<p<<endl;
    char* start = p;
    cout<< *start<<endl;
    int beginPos = 0;
    int endPos = 0;
    while(*start != '\0') {
        while( (*start) !='\0' && ((*start >='a' && *start <='z')||(*start >= 'A' && *start <= 'Z')) ) {
            ++start;
            ++endPos;
        }
        //这个时候说明遇到了单词一个，需要再反转回来
        reverseStr(p,beginPos,endPos);
        while( (*start) !='\0' && !((*start >='a' && *start <='z')||(*start >= 'A' && *start <= 'Z')) ) {
            ++start;
            ++endPos;
        }
        beginPos = endPos;
    } 
    return p;
}


char* reverseStr(char* str, int start,int end)
{
    if(NULL == str)return str;
    if(end > strlen(str)-1)end = strlen(str)-1;
    while(start < end) {
        //swap
        str[start] ^= str[end];
        str[end] ^= str[start];
        str[start] ^= str[end];
        ++start;
        --end;
    }
    return str;
}
